package org.jeecg.modules.charge.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.entity.charge.ChargeOrderReview;
import org.jeecg.entity.charge.ChargeOrderReviewDetail;
import org.jeecg.modules.charge.mapper.ChargeOrderReviewDetailMapper;
import org.jeecg.modules.charge.mapper.ChargeOrderReviewMapper;
import org.jeecg.modules.charge.service.IChargeOrderReviewService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 交易对账表
 * @Author: jeecg-boot
 * @Date:   2020-11-15
 * @Version: V1.0
 */
@Service
public class ChargeOrderReviewServiceImpl extends ServiceImpl<ChargeOrderReviewMapper, ChargeOrderReview> implements IChargeOrderReviewService {

	@Autowired
	private ChargeOrderReviewMapper chargeOrderReviewMapper;
	@Autowired
	private ChargeOrderReviewDetailMapper chargeOrderReviewDetailMapper;
	
	@Override
	@Transactional
	public void saveMain(ChargeOrderReview chargeOrderReview, List<ChargeOrderReviewDetail> chargeOrderReviewDetailList) {
		chargeOrderReviewMapper.insert(chargeOrderReview);
		for(ChargeOrderReviewDetail entity:chargeOrderReviewDetailList) {
			//外键设置
			entity.setOrderId(chargeOrderReview.getId());
			chargeOrderReviewDetailMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(ChargeOrderReview chargeOrderReview,List<ChargeOrderReviewDetail> chargeOrderReviewDetailList) {
		chargeOrderReviewMapper.updateById(chargeOrderReview);
		
		//1.先删除子表数据
		chargeOrderReviewDetailMapper.deleteByMainId(chargeOrderReview.getId());
		
		//2.子表数据重新插入
		for(ChargeOrderReviewDetail entity:chargeOrderReviewDetailList) {
			//外键设置
			entity.setOrderId(chargeOrderReview.getId());
			chargeOrderReviewDetailMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		chargeOrderReviewDetailMapper.deleteByMainId(id);
		chargeOrderReviewMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			chargeOrderReviewDetailMapper.deleteByMainId(id.toString());
			chargeOrderReviewMapper.deleteById(id);
		}
	}

	@Override
	public List<ChargeOrderReview> findPage(Page<ChargeOrderReview> page, ChargeOrderReview chargeOrderReview) {
		return chargeOrderReviewMapper.findPage(page,chargeOrderReview);
	}

	@Override
	public void batchSaveOrUpdate(List<ChargeOrderReview> chargeOrderReviewList) {
		chargeOrderReviewMapper.batchSaveOrUpdate(chargeOrderReviewList);
	}

}
